package com.wty.app.bthcar;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.wty.app.bthcar.R;
import com.wty.app.bthcar.utils.PreferenceUtil;

import java.util.LinkedHashMap;
import java.util.Map;

public class CodeSetttingActivity extends AppCompatActivity {
    Button bt_submit;
    EditText et_down;
    EditText et_left;
    EditText et_right;
    EditText et_stop;
    EditText et_up;
    LinearLayout layout_about;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        this.et_stop = (EditText) findViewById(R.id.et_stop);
        this.et_up = (EditText) findViewById(R.id.et_go);
        this.et_left = (EditText) findViewById(R.id.et_left);
        this.et_right = (EditText) findViewById(R.id.et_right);
        this.et_down = (EditText) findViewById(R.id.et_back);
        this.bt_submit = (Button) findViewById(R.id.btn_submit);
        this.layout_about = (LinearLayout) findViewById(R.id.layout_about);
        this.et_down.setText(PreferenceUtil.getInstance().getDownCode());
        this.et_up.setText(PreferenceUtil.getInstance().getUpCode());
        this.et_left.setText(PreferenceUtil.getInstance().getLeftCode());
        this.et_right.setText(PreferenceUtil.getInstance().getRightCode());
        this.et_stop.setText(PreferenceUtil.getInstance().getStopCode());
        this.bt_submit.setOnClickListener(new View.OnClickListener() {

            @Override // android.view.View.OnClickListener
            public void onClick(View v) {
                String result = CodeSetttingActivity.this.saveCode();
                if (!TextUtils.isEmpty(result)) {
                    Toast.makeText(CodeSetttingActivity.this, result,  Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(CodeSetttingActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                CodeSetttingActivity.this.finish();
            }
        });
        this.layout_about.setOnClickListener(new View.OnClickListener() {

            @Override // android.view.View.OnClickListener
            public void onClick(View v) {
            }
        });
    }

    private void saveToPreference(String down, String up, String left, String right, String stop) {
        PreferenceUtil.getInstance().writePreferences(PreferenceUtil.DOWN_CODE, down);
        PreferenceUtil.getInstance().writePreferences(PreferenceUtil.UP_CODE, up);
        PreferenceUtil.getInstance().writePreferences(PreferenceUtil.LEFT_CODE, left);
        PreferenceUtil.getInstance().writePreferences(PreferenceUtil.RIGHT_CODE, right);
        PreferenceUtil.getInstance().writePreferences(PreferenceUtil.STOP_CODE, stop);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private String saveCode() {
        String down = this.et_down.getText().toString();
        String up = this.et_up.getText().toString();
        String left = this.et_left.getText().toString();
        String right = this.et_right.getText().toString();
        String stop = this.et_stop.getText().toString();
        if (TextUtils.isEmpty(down) || TextUtils.isEmpty(up) || TextUtils.isEmpty(left) || TextUtils.isEmpty(right) || TextUtils.isEmpty(stop)) {
            return "编码不能为空";
        }
        Map<String, String> map = new LinkedHashMap<>();
        map.put(down, down);
        map.put(up, up);
        map.put(left, left);
        map.put(right, right);
        map.put(stop, stop);
        if (map.size() < 5) {
            return "编码不能相同";
        }
        saveToPreference(down, up, left, right, stop);
        return "";
    }
}
